(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:MzCtrl
     * @description
     * # MzCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('MzCtrl', MzCtrl);

    MzCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', '$state'];

    function MzCtrl($scope, dataService, helper, $rootScope, $location, $state) {
        //满赠活动列表
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList(1);

            $scope.$on('event:up-store-id', function() {
                $scope.store_id = $rootScope.store_id
                $scope.getList(1);
            });
        }

        $scope.getList = function(pageNo) {
            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.pageSize,
                store_id: $rootScope.store_id ? $rootScope.store_id : $scope.store_id
            };

            dataService.mzGet(query).then(function(list) {
                if (list) {
                    for (var i in list.mz_list) {
                        list.mz_list[i].diff_time = $scope.diff_time(list.mz_list[i].start_time, list.mz_list[i].end_time)
                    }
                    $scope.mz_list = list.mz_list;
                    $scope.total_count = list.total_count;
                }
            });
        }

        //js计算两个时间的时间差
        $scope.diff_time = function(start_time, end_time) {
            var s_time = new Date().getTime();
            var start = new Date(start_time).getTime();
            var end = new Date(end_time).getTime();

            //判断当前时间是否大于结束时间
            if (s_time > end) {
                return 0;
            }

            //判断当前时间是否大于开始时间
            if (s_time > start && s_time < end) {
                var time = new Date().getTime();
            } else {
                var time = new Date(start_time).getTime();
            }

            //var diff = end_time * 1000 - start_time * 1000 //时间差的毫秒数  
            var diff = end - time;

            //计算出相差天数  
            var days = Math.floor(diff / (24 * 3600 * 1000));

            //计算出小时数  
            var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数  
            var hours = Math.floor(leave1 / (3600 * 1000));
            //计算相差分钟数  
            var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数  
            var minutes = Math.floor(leave2 / (60 * 1000));

            //计算相差秒数  
            var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数  
            var seconds = Math.round(leave3 / 1000);

            var returnStr = seconds + "秒";
            if (minutes > 0) {
                returnStr = minutes + "分" + returnStr;
            }
            if (hours > 0) {
                returnStr = hours + "小时" + returnStr;
            }
            if (days > 0) {
                returnStr = days + "天" + returnStr;
            }
            return returnStr;
        }

        //满赠活动添加页面
        $scope.addAction = function() {
            var mz_activity_id = $location.search().mz_activity_id;

            if (mz_activity_id) {
                var query = { "store_id": $rootScope.store_id, "mz_activity_id": mz_activity_id };

                dataService.mzGet(query).then(function(result) {
                    if (result.total_count) {
                        $scope.mz_data = result.mz_list[0];
                        $scope.mz_activity_id = mz_activity_id;
                    }
                });
            } else {
                var data = {
                    "store_id": $rootScope.store_id, //门店id
                    "name": "满100元送券", //活动名称
                    "start_time": $scope.getDateStr(0), //活动开始时间
                    "end_time": $scope.getDateStr(30), //活动结束时间
                    "yuan": "", //满几元
                    "xid": "",
                    "type": "", //券类型
                    "event_type": "", //事件类型 1=交易支付成功后通知
                };
                $scope.mz_data = data;
            }
        }

        //满赠活动
        $scope.mzAdd = function(param) {
            if ($scope.mz_data.name.length > 10) {
                helper.myAlert("error:促销标题不能超过10个字");
                return false;
            }

            if ($scope.mz_data.end_time < $scope.mz_data.start_time) {
                helper.myAlert("error:开始时间不能大于结束时间");
                return false;
            }

            if ($scope.mz_data.yuan == 0) {
                helper.myAlert("优惠信息出错，请确认后保存");
                return false;
            }

            if (!$scope.mz_data.xid) {
                helper.myAlert("没有选择券，不能保存！");
                return false;
            }

            console.error("mz_data--->", $scope.mz_data);
            // return false;

            var mz_activity_id = $location.search().mz_activity_id;
            if (mz_activity_id) {
                dataService.mzUpdate($scope.mz_data).then(function(result) {
                    if (result.Msg == "ok") {
                        helper.myAlert("success", "#/mz/list");
                    } else {
                        helper.myAlert("error：" + result.Msg);
                        return false;
                    }
                });
            } else {
                if ($scope.mz_activity_id) {
                    $scope.mz_data.mz_activity_id = $scope.mz_activity_id;

                    dataService.mzUpdate($scope.mz_data).then(function(result) {
                        if (result.mz_activity_id) {
                            helper.myAlert("success", "#/mz/list");
                        } else {
                            helper.myAlert("error：" + result.Msg);
                            return false;
                        }
                    });
                } else {
                    $scope.oneAdd(param);
                }
            }
        }

        $scope.oneAdd = function() {
            dataService.mzAdd($scope.mz_data).then(function(result) {
                if (result.mz_activity_id) {
                    $scope.mz_activity_id = result.mz_activity_id;
                    helper.myAlert("success", "#/mz/list");
                } else {
                    helper.myAlert("error：" + result.Msg);
                    return false;
                }
            });
        }

        //满赠活动详情
        $scope.detailAction = function() {
            $scope.pageSize = 10;

            var mz_activity_id = $scope.mz_activity_id = $location.search().mz_activity_id;

            if (mz_activity_id) {
                $scope.getDetailList(1);
            } else {
                helper.myAlert("没有满赠活动编号，无法查询数据");
                return false;
            }
        }

        $scope.getDetailList = function(pageNo){
            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.pageSize,
                mz_activity_id: $scope.mz_activity_id
            };

            dataService.mzDetailGet(query).then(function(list) {
                if (list) {
                    $scope.mz_detail_list = list.mz_detail_list;
                    $scope.detail_count = list.total_count;
                }
            });
        }

        //活动时间计算
        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }

            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }
    }
})();